package MRtest;

import MRtest.Bean;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

public class Red extends Reducer<Text, Bean, Text, Bean> {
    @Override
    protected void reduce(Text key, Iterable<Bean> values, Reducer<Text, Bean, Text, Bean>.Context context) throws IOException, InterruptedException {
        BigDecimal cost_total = BigDecimal.ZERO;
        String time_all = "";
        List<Long> timeList = new ArrayList<>();
        for(Bean va : values){
            BigDecimal cost = va.getCost();
            cost_total=cost_total.add(cost);
            time_all = time_all+" "+va.getTime();
            long time = Long.parseLong(va.getTime());
//            time *= 1000;
            //将毫秒数值保存在timeList中
            timeList.add(time);
        }
//        List<String> timeArr = new ArrayList<>();
//        timeList.sort(null);
//        for (long timeTmp : timeList) {
//            Instant instant = Instant.ofEpochMilli(timeTmp);
//            LocalDateTime date = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
//            timeArr.add(date.toString());
//        }
//        String timePrint;
//        timePrint = String.join("\t", timeArr);
        Bean bean = new Bean(key.toString(),time_all,cost_total);
        context.write(key,bean);
    }
}
